package com.android.zweibo.logic;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;

import com.android.zweibo.bean.Common;
import com.android.zweibo.bean.UserInfo;
import com.android.zweibo.db.DBHelper;

public class UserInfoServices {
    
	private DBHelper dbHelper;
	private String[] columns = new String[] {UserInfo.ID,UserInfo.USER_ID,UserInfo.USER_NAME,
            UserInfo.TOKEN,UserInfo.TOKEN_SECRET,
            UserInfo.IS_DEFAULT,UserInfo.USER_ICON};
	
	public UserInfoServices(Context context) {
	    dbHelper = new DBHelper(context);	
	}
	
	/**
	 * 插入userinfo
	 * @param user
	 */
	public  void insertUserInfo(UserInfo user){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(UserInfo.USER_ID,user.getUserId());
		values.put(UserInfo.USER_NAME, user.getUserName());
		values.put(UserInfo.TOKEN, user.getToken());
		values.put(UserInfo.TOKEN_SECRET, user.getTokenSecret());
		values.put(UserInfo.IS_DEFAULT, user.getIsDefault());
		
		db.insert(Common.USER_INFO_TB_NAME, null, values);
		db.close();
	}
	
	/**
	 * 根据用户Id查找userInfo
	 * @param id
	 * @return
	 */
	public UserInfo findUserByID(String id) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(Common.USER_INFO_TB_NAME,columns , UserInfo.USER_ID + "=?", new String[]{id}, null, null,null);
		UserInfo user  = null;
		if(null != cursor) {
			if(cursor.getCount() > 0) {
				cursor.moveToFirst();
				user= new UserInfo();
				user.setId(cursor.getLong(cursor.getColumnIndex(UserInfo.ID)));
				user.setUserId(cursor.getString(cursor.getColumnIndex(UserInfo.USER_ID)));
				user.setUserName(cursor.getString(cursor.getColumnIndex(UserInfo.USER_NAME)));
				user.setToken(cursor.getString(cursor.getColumnIndex(UserInfo.TOKEN)));
				user.setTokenSecret(cursor.getString(cursor.getColumnIndex(UserInfo.TOKEN_SECRET)));
				user.setIsDefault(cursor.getString(cursor.getColumnIndex(UserInfo.IS_DEFAULT)));
			}
		}
		cursor.close();
		db.close();
		return user;
	}
	
    /**
     * 查询全部用户
     * @return
     */
	public List<UserInfo> findAllUser() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(Common.USER_INFO_TB_NAME, columns, null, null, null, null, null);
		List<UserInfo> userList =null;
		if( null != cursor && cursor.getCount() >0) {
			userList =  new ArrayList<UserInfo>();
			UserInfo  user = null;
			while(cursor.moveToNext()) {
				user= new UserInfo();
				user.setId(cursor.getLong(cursor.getColumnIndex(UserInfo.ID)));
				user.setUserId(cursor.getString(cursor.getColumnIndex(UserInfo.USER_ID)));
				user.setUserName(cursor.getString(cursor.getColumnIndex(UserInfo.USER_NAME)));
				user.setToken(cursor.getString(cursor.getColumnIndex(UserInfo.TOKEN)));
				user.setTokenSecret(cursor.getString(cursor.getColumnIndex(UserInfo.TOKEN_SECRET)));
				user.setIsDefault(cursor.getString(cursor.getColumnIndex(UserInfo.IS_DEFAULT)));
				byte[] byteImage = cursor.getBlob(cursor.getColumnIndex(UserInfo.USER_ICON));
				if(null != byteImage) {
					ByteArrayInputStream is = new ByteArrayInputStream(byteImage);
					Drawable icon = Drawable.createFromStream(is, "image");
					user.setUserIcon(icon);
				}
				userList.add(user);
			}
		}
		cursor.close();
		db.close();
//		System.out.println("mainSerivce  - userList" + userList.size());
		return userList;
	}
	
	
    /**
     * 更新用户的token信息
     * @param user
     */
	public void updateUserInfo(UserInfo user) {
		  SQLiteDatabase db = dbHelper.getWritableDatabase();
		  ContentValues values = new ContentValues();
		  values.put(UserInfo.TOKEN,user.getToken());
		  values.put(UserInfo.TOKEN_SECRET, user.getTokenSecret());
		  db.update(Common.USER_INFO_TB_NAME, values, UserInfo.USER_ID + "=?", new String[]{user.getUserId()});
		  db.close();
	}
    
	/**
	 * 更新用户的头像，名称
	 * @param userID
	 * @param userName
	 * @param icon
	 */
    public void updateUserInfo(String userID,String userName,Bitmap icon) {
    	
    	SQLiteDatabase db = dbHelper.getWritableDatabase();
    	ContentValues values = new ContentValues();
    	values.put(UserInfo.USER_NAME, userName);
    	//将bitmap 转换成字节数组
    	ByteArrayOutputStream os = new ByteArrayOutputStream();
    	icon.compress(CompressFormat.PNG, 100, os);
    	values.put(UserInfo.USER_ICON, os.toByteArray());
    	
    	db.update(Common.USER_INFO_TB_NAME, values, UserInfo.USER_ID + "=?", new String[]{userID});
    	db.close();
    } 
}
